#ifndef __DIS_IMG_PRO_H_
#define __DIS_IMG_PRO_H_
#include <stdio.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/epoll.h>
#include <pthread.h>
#include <sqlite3.h>
#include <signal.h>
#include <sys/wait.h>
#include <dirent.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
 #include <sys/sendfile.h>
 #include <limits.h>
#define ERROR_CHECK_FUN(ret, num) \
    {                             \
        if (ret == num)           \
        {                         \
            return -1;            \
        }                         \
    }
// 指令宏
// TASK
#define TASK_NEW  "<--new_task_-->"
#define GET_CLIENT "<--get_the_online_client-->"
#define TASK_UP "<--task_distribution-->"
#define TASK_DOWN "<--Task_acceptance-->"
#define TASK_RES "<--Task_result_upload-->"
#define TASK_ON "<--Task_ready-->"
#define TASK_SND_OVER "<--Task_send_over-->"
// USER
#define USER_LOG "<--user_log-->"
#define LOG_SUCCESS "<--user_log_success-->"
#define LOG_FAILED "<--user_log_failed-->"
#define USER_CHANGE "<--user_change-->"
#define CHA_SUCCESS "<--user_cha_success-->"
#define CHA_FAILED "<--user_cha_failed-->"
#define USER_REGISTER "<--user_register-->"
#define REG_SUCCESS "<--user_reg_success-->"
#define REG_FAILED "<--user_reg_failed-->"
#define USER_DEL "<--user_delete-->"
#define DEL_SUCCESS "<--user_del_success-->"
#define DEL_FAILED "<--user_del_failed-->"

#define ARGS_CHECK(argc, num)                 \
    {                                         \
        if (argc != num)                      \
        {                                     \
            fprintf(stderr, "args error!\n"); \
            return -1;                        \
        }                                     \
    }
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            exit(1);               \
        }                          \
    }
#define ERROR_CHECK_SQL(ret, num, msg, db)                     \
    {                                                          \
        if (ret != num)                                        \
        {                                                      \
            fprintf(stderr, "%s:%s", msg, sqlite3_errmsg(db)); \
            exit(1);                                           \
        }                                                      \
    }
#define ERROR_CHECK_SND(ret1, ret2, num, msg) \
    {                                         \
        if (ret1 == num)                      \
        {                                     \
            perror(msg);                      \
            break;                            \
        }                                     \
        else if (ret2 == num)                 \
        {                                     \
            perror(msg);                      \
            break;                            \
        }                                     \
    }
// threadpool
typedef struct task_s
{
    int netfd;
    struct task_s *pNext;
} tasks;
typedef struct taskQueue_s
{
    tasks *pFront;
    tasks *pRear;
    int size;
    pthread_mutex_t mutex;
    pthread_cond_t cond;
} taskQueues;
typedef struct threadPool_s
{
    pthread_t *pTid;
    int thread_num;
    taskQueues taskQueue;
    taskQueues task_online;
    int exitFlag;
} threadPool;
typedef struct data_package
{
    int len;
    char buf[1024];
} PK;
typedef struct train
{
    int length;
    char buf[1024];
} train_t;
int taskEnqueue(taskQueues *pTaskQueue, int netfd);
int taskDequeue(taskQueues *pTaskQueue);
int threadPoolInit(threadPool *pThreadPool, int worker_num);
int makeWorker(threadPool *pThreadPool);

//
// net
/**
 * @brief
 *
 * @param ip 监听进程IP地址
 * @param port  进程端口号
 * @param listens
 * @return int
 */
int tcp_init(const char *ip, int port, int listens);
int tcp_wait(int fd_listen);
int epollAdd(int fd, int epfd);
int epollDel(int fd, int epfd);
 int myServer_task(int netFd);

int recvn(int sockFd, void *pstart, int len);
// userSys

int sql_table_val_create();
int sql_table_val_delete();
int sql_table_val_alter();
int sql_table_val_select();
 // task
typedef struct uploaded_imgs
{
    char buf[256];
    int ID;
    struct uploaded_imgs *pNext;

} up_img;
typedef struct task_stat
{
    up_img *pFront;
    up_img *pRear;
    int size;
} st;

int ImgEnqueue(st *pImgQueue, char *filename,int ID);
int ImgDequeue(st *pImgQueue);
void uploadingTask(int net_fd);
#endif
